#!/usr/bin/env python

#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""
Create support tables for search. Includes listing of repositories and languages.
"""

# imports
from querymgr import *
from utility import *
import re

# create reposit class
class createRepList:
	def __init__(self):
		pass

	def create_repository(self):
		# clear table
		self.cc_bar = progress("Creating Repository List...")
		self.cc_bar.begin()

		# clear and remove old table
		dbquery("DROP TABLE IF EXISTS harvestRepository;")

		self.init_repository()

		results = dbquery("SELECT iRepository FROM harvestSearch " + \
			"GROUP BY iRepository;")

		if results is not None:
			for result in results:
				insertDict = {}
				insertDict['repositoryName'] = result['iRepository'].strip()

				# insert
				dbinsertsafe('harvestRepository', insertDict)

		self.cc_bar.done()

	def init_repository(self):
		# create new table for repositories
		dbquery("CREATE TABLE harvestRepository (" + \
			"recID INT AUTO_INCREMENT NOT NULL PRIMARY KEY," + \
			"repositoryName TEXT NOT NULL" + \
			") CHARACTER SET utf8;")
		

# create reposit class
class createLangList:
	def __init__(self):
		pass

	def init_language(self):
		# create new table for repositories
		dbquery("CREATE TABLE harvestLanguage (" + \
			"recID INT AUTO_INCREMENT NOT NULL PRIMARY KEY," + \
			"langCode VARCHAR(8) NOT NULL," + \
			"langDesc TEXT NOT NULL," + \
			"langComm VARCHAR(8) NOT NULL" + \
			") CHARACTER SET utf8;")

	def create_language(self):
		# clear table
		self.cc_bar = progress("Creating Language List...")
		self.cc_bar.begin()

		# clear and remove old table
		dbquery("DROP TABLE IF EXISTS harvestLanguage;")

		self.init_language()

		self.load_languages()

		self.cc_bar.done()

	def load_languages(self):
		langfile = open('lang_codes_3char','r')
		
		# parse line re
		par = re.compile(r'(\w\w\w)?/?(\w\w\w)?\s+(\w\w)?/?(\w\w)?-?\s+(.+?)$')

		langDict = {}

		for line in langfile:
			match = par.search(line)
			result = list(match.groups())
			# decide on a code
			for m in range(4,-1,-1):
				if result[m] is not None:
					final = result[m]
			# all codes point to the desired code
			for abr in range(0,4):
				if result[abr] is not None:
					langDict[result[abr]] = (result[4],final)

		langfile.close()
		
		for k in langDict:
			v1,v2 = langDict[k]

			insertDict = {}
			insertDict['langCode'] = k
			insertDict['langDesc'] = v1
			insertDict['langComm'] = v2

			dbinsertsafe('harvestLanguage', insertDict)
		

	def parse_language(self):
		# clear table
		self.cc_bar = progress("Creating Language Active List...")
		self.cc_bar.begin()

		# clear and remove old table
		dbquery("DROP TABLE IF EXISTS harvestLanguageActive;")

		# create new table for repositories
		dbquery("CREATE TABLE harvestLanguageActive (" + \
			"recID INT AUTO_INCREMENT NOT NULL PRIMARY KEY," + \
			"langCode VARCHAR(8) NOT NULL," + \
			"langDesc TEXT NOT NULL" + \
			") CHARACTER SET utf8;")

		langDict = {}

		results = dbquery("SELECT cLanguage FROM harvestSearch " + \
			"GROUP BY cLanguage;")

		active = {}

		if results is not None:
			for result in results:
				langSplit = result['cLanguage'].split(' ')

				for lng in langSplit:
					if len(lng) > 1:  active[lng] = lng

			# insert active languages
			active = list(active)

			for lng in active:
				insert = ("INSERT INTO harvestLanguageActive (langCode,langDesc) "
					"VALUES('%s',(SELECT DISTINCT(langDesc) FROM harvestLanguage " 
					"WHERE langCode = '%s'));" % (lng,lng) )

				dbquery(insert)

		self.cc_bar.done()

# the main class
def main():
	rep = createRepList()

	rep.create_repository()

	lang = createLangList()

	lang.create_language()

	lang.parse_language()

# file is being executed, start main
if __name__ == '__main__':
	main()

# eof
